package Formularios;

import java.sql.ResultSet;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

public class viewAlterarFunc extends javax.swing.JPanel {

    /** Creates new form viewAlterarFunc */
    public viewAlterarFunc() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jComboBox1 = new javax.swing.JComboBox();
        textFieldNome = new java.awt.TextField();
        textFieldCPF = new javax.swing.JFormattedTextField();
        labelTelefone = new java.awt.Label();
        textFieldTelefone = new javax.swing.JFormattedTextField();
        label1 = new java.awt.Label();
        labelCTPS = new java.awt.Label();
        labelSalario = new java.awt.Label();
        textAreaEndereco = new java.awt.TextArea();
        labelNome = new java.awt.Label();
        label2 = new java.awt.Label();
        labelCTPS1 = new java.awt.Label();
        labelCTPS2 = new java.awt.Label();
        jComboBox2 = new javax.swing.JComboBox();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        textFieldCTPS = new javax.swing.JFormattedTextField();
        textFieldSalario = new javax.swing.JFormattedTextField();
        jLabel1 = new javax.swing.JLabel();

        setPreferredSize(new java.awt.Dimension(672, 400));
        addAncestorListener(new javax.swing.event.AncestorListener() {
            public void ancestorMoved(javax.swing.event.AncestorEvent evt) {
            }
            public void ancestorAdded(javax.swing.event.AncestorEvent evt) {
                formAncestorAdded(evt);
            }
            public void ancestorRemoved(javax.swing.event.AncestorEvent evt) {
            }
        });

        jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Exibir Funcionários" }));
        jComboBox1.setSelectedItem(null);
        jComboBox1.setAutoscrolls(true);
        jComboBox1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                //jComboBox1MouseClicked(evt);
            }
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                //jComboBox1MouseEntered(evt);
            }
            public void mousePressed(java.awt.event.MouseEvent evt) {
            //    jComboBox1MousePressed(evt);
            }
            public void mouseReleased(java.awt.event.MouseEvent evt) {
              //  jComboBox1MouseReleased(evt);
            }
        });
        jComboBox1.addComponentListener(new java.awt.event.ComponentAdapter() {
            public void componentShown(java.awt.event.ComponentEvent evt) {
                //jComboBox1ComponentShown(evt);
            }
        });
        jComboBox1.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                //jComboBox1ItemStateChanged(evt);
            }
        });
        jComboBox1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jComboBox1ActionPerformed(evt);
            }
        });
        jComboBox1.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                //jComboBox1FocusGained(evt);
            }
            public void focusLost(java.awt.event.FocusEvent evt) {
                //jComboBox1FocusLost(evt);
            }
        });
        jComboBox1.addInputMethodListener(new java.awt.event.InputMethodListener() {
            public void caretPositionChanged(java.awt.event.InputMethodEvent evt) {
            }
            public void inputMethodTextChanged(java.awt.event.InputMethodEvent evt) {
                //jComboBox1InputMethodTextChanged(evt);
            }
        });

        textFieldNome.setCursor(new java.awt.Cursor(java.awt.Cursor.TEXT_CURSOR));

        try {
            textFieldCPF.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("###.###.###-##")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }

        labelTelefone.setText("Telefone:");

        try {
            textFieldTelefone.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("(##) #### - ####")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }

        label1.setAlignment(java.awt.Label.CENTER);
        label1.setFont(new java.awt.Font("Dialog", 1, 18));
        label1.setText("Alterar Funcionários");

        labelCTPS.setText("Serviço:");

        labelSalario.setText("Salario:");

        labelNome.setText("Nome:");

        label2.setAlignment(java.awt.Label.CENTER);
        label2.setFont(new java.awt.Font("Dialog", 1, 12));
        label2.setText("Escolha o Funcionário que deseja alterar");

        labelCTPS1.setText("CTPS:");

        labelCTPS2.setText("Endereço:");

        jButton1.setText("Alterar");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setText("Limpar");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        try {
            textFieldCTPS.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("##.###.#####/##")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }

        textFieldSalario.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.NumberFormatter(new java.text.DecimalFormat("#0"))));

        jLabel1.setText("CPF");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(137, 137, 137)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                .addComponent(labelTelefone, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(labelNome, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(labelCTPS, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                        .addComponent(labelCTPS2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addComponent(labelCTPS1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                            .addGap(18, 18, 18)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                        .addComponent(textFieldCTPS, 0, 0, Short.MAX_VALUE)
                                        .addComponent(textFieldTelefone, javax.swing.GroupLayout.PREFERRED_SIZE, 96, javax.swing.GroupLayout.PREFERRED_SIZE))
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 329, Short.MAX_VALUE)
                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(labelSalario, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addComponent(jLabel1))
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                        .addComponent(textFieldSalario, 0, 0, Short.MAX_VALUE)
                                        .addComponent(textFieldCPF, javax.swing.GroupLayout.PREFERRED_SIZE, 92, javax.swing.GroupLayout.PREFERRED_SIZE))
                                    .addGap(17, 17, 17))
                                .addComponent(textAreaEndereco, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 590, Short.MAX_VALUE)
                                .addGroup(layout.createSequentialGroup()
                                    .addComponent(jButton2)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                    .addComponent(jButton1))
                                .addComponent(textFieldNome, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 577, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(jComboBox2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 183, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGroup(layout.createSequentialGroup()
                            .addGap(116, 116, 116)
                            .addComponent(label2, javax.swing.GroupLayout.PREFERRED_SIZE, 264, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, 278, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(261, 261, 261)
                        .addComponent(label1, javax.swing.GroupLayout.PREFERRED_SIZE, 293, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 114, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(217, 217, 217))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(32, 32, 32)
                .addComponent(label1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(28, 28, 28)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(label2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(37, 37, 37)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(labelNome, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(textFieldNome, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(28, 28, 28)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(labelTelefone, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(textFieldTelefone, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(textFieldCPF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel1)))
                .addGap(21, 21, 21)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(labelCTPS1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(labelSalario, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(textFieldCTPS, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(textFieldSalario, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(textAreaEndereco, javax.swing.GroupLayout.PREFERRED_SIZE, 72, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(labelCTPS2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(14, 14, 14)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(labelCTPS, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jButton1)
                            .addComponent(jButton2)))
                    .addComponent(jComboBox2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        labelCTPS.getAccessibleContext().setAccessibleName("Endereco");
        label2.getAccessibleContext().setAccessibleName("Escolha um funcionário para alterar");
    }// </editor-fold>//GEN-END:initComponents

    private void jComboBox1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboBox1ActionPerformed
        //
        try {
            if (jComboBox1.getSelectedItem() != "") {
                String retorno = jComboBox1.getSelectedItem().toString().substring(0, 14);
                int id = SalaoApp.controle.buscarIdFuncionario(retorno);

                ResultSet result = SalaoApp.controle.consultaFuncionario();

                while (result.next()) {
                    if (Integer.parseInt(result.getString(1)) == id) {
                        textFieldNome.setText(result.getString(2));
                        textFieldCPF.setText(result.getString(5));
                        textFieldCTPS.setText(result.getString(6));
                        textFieldTelefone.setText(result.getString(3));
                        textFieldSalario.setText(result.getString(8));
                        textAreaEndereco.setText(result.getString(7));
                        String servico = null;
                        ResultSet rs = SalaoApp.controle.consultaServico();
                        while (rs.next()) {
                            if (Integer.parseInt(rs.getString(1)) == Integer.parseInt(result.getString(9))) {
                                servico = rs.getString(2);
                                break;
                            }
                        }
                        jComboBox2.setSelectedItem(servico);
                    }
                }
            } else {
                limparCampos();
            }

        } catch (Exception exception) {
            exception.printStackTrace();
            System.exit(1);
        }
	}//GEN-LAST:event_jComboBox1ActionPerformed
    private void limparCampos() {
        textFieldNome.setText("");
        textFieldCPF.setText("");
        textFieldCTPS.setText("");
        textFieldTelefone.setText("");
        textFieldSalario.setText("");
        textAreaEndereco.setText("");
        textFieldCPF.setEditable(false);


    }

    public void preencherLista() {
        try {

            ResultSet rs = SalaoApp.controle.consultaFuncionario();

            DefaultComboBoxModel model = new DefaultComboBoxModel();

            jComboBox1.setModel(model);
            jComboBox1.addItem("");
            while (rs.next()) {
                String nomeTelefone = rs.getString(5) + " - " + rs.getString(2);
                jComboBox1.addItem(nomeTelefone);
            }

            rs = SalaoApp.controle.consultaServico();

            model = new DefaultComboBoxModel();

            jComboBox2.setModel(model);


            while (rs.next()) {
                //String nomeTelefone = rs.getString(2) + " - " + rs.getString(3);
                jComboBox2.addItem(rs.getString(2));
            }

            rs.close();

        } catch (Exception exception) {
            exception.printStackTrace();
            System.exit(1);
        }
    }
    private void formAncestorAdded(javax.swing.event.AncestorEvent evt) {//GEN-FIRST:event_formAncestorAdded
        // TODO add your handling code here:
        limparCampos();
        preencherLista();
    }//GEN-LAST:event_formAncestorAdded

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:

        String nome = textFieldNome.getText();
        String telefone = textFieldTelefone.getText();
        String cpf = textFieldCPF.getText();
        String ctps = textFieldCTPS.getText();
        float salario = Float.parseFloat(textFieldSalario.getText());
        String endereco = textAreaEndereco.getText();
        String servico = jComboBox2.getSelectedItem().toString();
        /*if (SalaoApp.controle.existeFuncionario(cpf)) {
        SalaoApp.mostraMensagem("Já existe um funcionário cadastrado com esse CPF!", "Cadastro Funcionarário");
        } else*/ if (textFieldNome.getText().length() < 4 || textFieldNome.getText().length() > 60) {
            SalaoApp.mostraMensagem("Verifique o nome digitado, ele deve conter entre 4 e 60 caracteres!", "Cadastro Funcionario");
        } else if (textFieldCPF.getText().length() < 14 || textFieldCPF.getText().contains(" ") || textFieldCPF.getText().equals("   .   .   -  ")) {
            SalaoApp.mostraMensagem("Informe todos os número do CPF!", "Cadastro Funcionario");
        } else if (textFieldCTPS.getText().length() < 15 || textFieldCTPS.getText().contains(" ") || textFieldCTPS.getText().equals("  .   .     /  ")) {
            SalaoApp.mostraMensagem("CTPS deve conter todos os dígitos!", "Cadastro Funcionario");
        } else if (textFieldSalario.getText() == null || Integer.parseInt(textFieldSalario.getText()) < 0) {
            SalaoApp.mostraMensagem("Informe um salário!", "Cadastro Funcionario");
        } else if (textFieldSalario.getText().contains(".")) {
            SalaoApp.mostraMensagem("O salário não deve conter '.'! Informe o salário sem as casas decimais!", "Cadastro Funcionario");
        } else if (textFieldSalario.getText().length() > 4 || textFieldSalario.getText().length() == 0) {
            SalaoApp.mostraMensagem("O salário está incorreto!", "Cadastro Funcionario");
        } else if (textFieldTelefone.getText().length() < 15 || textFieldTelefone.getText().equals("(  )      -     ")) {
            SalaoApp.mostraMensagem("O Telefone deve ser preenchido por completo!", "Cadastro Funcionario");
        } else if (textAreaEndereco.getText().length() > 99) {
            SalaoApp.mostraMensagem("O Endereço não pode ultrapassar os 99 caracteres!", "Cadastro Funcionario");
        } else if (jComboBox1.getSelectedItem() != "") {
            String retorno = jComboBox1.getSelectedItem().toString().substring(0, 14);
            int id = SalaoApp.controle.buscarIdFuncionario(retorno);
            int codServico = SalaoApp.controle.buscarIdServico(servico);



            if (SalaoApp.controle.atualizarFuncionario(textFieldNome.getText(), textFieldTelefone.getText(),
                    textAreaEndereco.getText(), textFieldCPF.getText(), textFieldCTPS.getText(), Integer.parseInt(textFieldSalario.getText()), codServico, id) != 0) {
                SalaoApp.mostraMensagem("Dados Alterados!", "Alterar Funcionário");
                limparCampos();
                jComboBox1.setSelectedItem("");
                preencherLista();

            }
        } else {
            JOptionPane.showMessageDialog(null, "Você deve ter selecionado pelo menos uma pessoa!");
        }

    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        // TODO add your handling code here:
        limparCampos();
        jComboBox1.setSelectedItem(0);
    }//GEN-LAST:event_jButton2ActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JComboBox jComboBox2;
    private javax.swing.JLabel jLabel1;
    private java.awt.Label label1;
    private java.awt.Label label2;
    private java.awt.Label labelCTPS;
    private java.awt.Label labelCTPS1;
    private java.awt.Label labelCTPS2;
    private java.awt.Label labelNome;
    private java.awt.Label labelSalario;
    private java.awt.Label labelTelefone;
    private java.awt.TextArea textAreaEndereco;
    private javax.swing.JFormattedTextField textFieldCPF;
    private javax.swing.JFormattedTextField textFieldCTPS;
    private java.awt.TextField textFieldNome;
    private javax.swing.JFormattedTextField textFieldSalario;
    private javax.swing.JFormattedTextField textFieldTelefone;
    // End of variables declaration//GEN-END:variables
}
